package com.nopay.cdxphone.net;

import android.graphics.Bitmap;
import android.util.Log;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.lzy.okgo.callback.FileCallback;
import com.nopay.cdxphone.bean.CommonResponse;
import com.nopay.cdxphone.net.callback.CustomBitmapCallback;
import com.nopay.cdxphone.net.callback.JsonCallback;

import java.io.File;
import java.util.Map;

/**
 * OkGo二次封装
 */
public class OkGoUtil {

    public static <T> void get(String url, Object tag, Map<String, String> params, JsonCallback<T> callback) {

        OkGo.<T>get(url)
                .tag(tag)
                .params(params)
                .execute(callback);
    }

    public static <T extends CommonResponse> void post(String url, Object tag, Map<String, String> params, JsonCallback<T> callback) {
        Log.d("OkGoUtil", "method post url" + url);
        OkGo.<T>post(url)
                .tag(tag)
                .params(params)
                .execute(callback);
    }

    public static <T extends CommonResponse> void postJson(String url, Object tag, Map<String, String> params, JsonCallback<T> callback) {
        OkGo.<T>post(url)
                .tag(tag)
                .upJson(new Gson().toJson(params))
                .execute(callback);
    }

    public static <T extends CommonResponse> void postJson(String url, Object tag, String jsonParams, JsonCallback<T> callback) {
        OkGo.<T>post(url)
                .tag(tag)
                .upJson(jsonParams)
                .execute(callback);
    }

    public static void getBitmap(String url, Object tag, BitmapCallback callback) {

        OkGo.<Bitmap>get(url)
                .tag(tag)
                .execute(callback);
    }

    public static void getBitmap(String url, Object tag, CustomBitmapCallback callback) {
        OkGo.<Bitmap>get(url)
                .tag(tag)
                .execute(callback);
    }

    public static <T extends CommonResponse> void postFiles(String url, Object tag, Map<String, String> params, File file, JsonCallback<T> callback) {

        OkGo.<T>post(url)
                .isMultipart(true)
                .tag(tag)
                .params(params)
                .params("file", file)
                .execute(callback);
    }

    public static <T extends CommonResponse> void postFiles(String url, Object tag, Map<String, String> params, JsonCallback<T> callback) {

        OkGo.<T>post(url)
                .isMultipart(true)
                .tag(tag)
                .params(params)
                .execute(callback);

    }

    public static void downloadFile(String url, Object tag, FileCallback callback) {
        OkGo.<File>get(url)
                .tag(tag)
                .execute(callback);
    }
}
